package com.loadburn.heron.bind;

import com.loadburn.heron.render.AdditionAttributes;
import com.loadburn.heron.render.RedirectAttributes;
import net.jcip.annotations.NotThreadSafe;

import java.util.*;

/**
 * @author slacrey (scstlinfeng@yahoo.com)
 *         Date: 13-10-16
 */
@NotThreadSafe
public class DefaultResponse implements Response {

    private static final String TEXT_HTML = "text/html;charset=utf-8";
    private Object page;
    private AdditionAttributes additionAttributes;
    private RedirectAttributes redirectAttributes;

    private List<String> errors;

    @SuppressWarnings("unchecked")
    public DefaultResponse(Object context) {
        this.page = context;
    }

    private final StringBuilder out = new StringBuilder();

    private String redirect;

    @Override
    public Object getPageInstances() {
        return this.page;
    }

    public void write(String text) {
        out.append(text);
    }

    public void redirect(String to) {
        this.redirect = to;
    }

    public String getRedirect() {
        return redirect;
    }

    public String getContentType() {
        return TEXT_HTML;
    }

    public void clear() {
        out.delete(0, out.length());
    }

    @Override
    public void setErrors(List<String> errors) {
        this.errors = errors;
    }

    public List<String> getErrors() {
        return errors;
    }

    @Override
    public String toString() {
        return out.toString();
    }

    @Override
    public void setRedirectAttributes(RedirectAttributes redirectAttributes) {
        this.redirectAttributes = redirectAttributes;
    }

    @Override
    public RedirectAttributes getRedirectAttributes() {
        return this.redirectAttributes;
    }

    @Override
    public void setAdditionAttributes(AdditionAttributes additionAttributes) {
        this.additionAttributes = additionAttributes;
    }

    @Override
    public AdditionAttributes getAdditionAttributes() {
        return this.additionAttributes;
    }
}
